//
//  RHMQCommandListController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/8/15.
//

#import "RHMQCommandListController.h"
#import "RHMQCommandInfoController.h"
#import "RHMQTTManage.h"
#import "RHMQDevModel.h"
#import "RHMQMsgModel.h"

@interface RHMQCommandListController ()<UITableViewDelegate ,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;

@property (nonatomic ,assign) __block NSInteger selectIndex;

@end

@implementation RHMQCommandListController

- (NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"接收指令"];
    [self.view setBackgroundColor:CFTabViewBgColor];
    [self setupUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noticNewCommandInfo:) name:DeviecNewMsgNotification object:nil];
}


- (void)setupUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:CFTabViewBgColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

#pragma mark - NSNotification
- (void)noticNewCommandInfo:(NSNotification *)notification{
    [self.tableView reloadData];
}


#pragma mark - UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [RHMQTTManage shareMQTTManage].devModel.commandsArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    return footView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
    [cell.textLabel setTextColor:kCOLOR_HEX(@"666666")];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
    QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
    [btn setTitleColor:kCOLOR_HEX(@"666666") forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [btn setImagePosition:QMUIButtonImagePositionRight];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [btn setFrame:CGRectMake(0, 0, 200, 40)];
    [cell setAccessoryView:btn];
    [btn setEnabled:NO];
    
    RHMQMsgModel *msgModel = [RHMQTTManage shareMQTTManage].devModel.commandsArray[indexPath.row];
    [cell.textLabel setText:[NSString stringWithFormat:@"指令:%@",msgModel.msgType]];
    [btn setTitle:msgModel.time forState:UIControlStateNormal];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RHMQMsgModel *msgModel = [RHMQTTManage shareMQTTManage].devModel.commandsArray[indexPath.row];
    RHMQCommandInfoController *vc = [[RHMQCommandInfoController alloc] initWithMsg:msgModel];
    [self.navigationController pushViewController:vc animated:YES];
}


@end
